package com.stylefeng.guns.modular.party.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import com.stylefeng.guns.core.common.exception.BizExceptionEnum;
import com.stylefeng.guns.core.exception.GunsException;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.util.ToolUtil;

import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.system.model.PartDiscipline;
import com.stylefeng.guns.modular.system.model.Student;
import com.stylefeng.guns.modular.system.warpper.PartDisciplineWrapper;
import com.stylefeng.guns.modular.system.warpper.StudentWrapper;
import com.stylefeng.guns.modular.party.service.IPartDisciplineService;

/**
 * 党风党纪管理控制器
 *
 * @author fengshuonan
 * @Date 2018-11-26 02:54:30
 */
@Controller
@RequestMapping("/partDiscipline")
public class PartDisciplineController extends BaseController {

    private String PREFIX = "/party/partDiscipline/";

    @Autowired
    private IPartDisciplineService partDisciplineService;

    /**
     * 跳转到党风党纪管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "partDiscipline.html";
    }

    /**
     * 跳转到添加党风党纪管理
     */
    @RequestMapping("/partDiscipline_add")
    public String partDisciplineAdd() {
        return PREFIX + "partDiscipline_add.html";
    }

    /**
     * 跳转到修改党风党纪管理
     */
    @RequestMapping("/partDiscipline_update/{partDisciplineId}")
    public String partDisciplineUpdate(@PathVariable Integer partDisciplineId, Model model) {
        PartDiscipline partDiscipline = partDisciplineService.selectById(partDisciplineId);
        model.addAttribute("partDiscipline",partDiscipline);
        LogObjectHolder.me().set(partDiscipline);
        return PREFIX + "partDiscipline_edit.html";
    }

    /**
     * 获取党风党纪管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String name, @RequestParam(required = false) String beginTime, @RequestParam(required = false) String endTime) {
    	//分页
    	Page<PartDiscipline> page = new PageFactory<PartDiscipline>().defaultPage();
    	List<Map<String,Object>> partDisciplineList = partDisciplineService.selectpartDisciplines(page,name,beginTime,endTime,page.getOrderByField(),page.isAsc());
        page.setRecords((List<PartDiscipline>)new PartDisciplineWrapper(partDisciplineList).warp());
    	return super.packForBT(page);
    }

    /**
     * 新增党风党纪管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(PartDiscipline partDiscipline) {
    	partDiscipline.setCreatetime(new Date());
    	partDiscipline.setSort(partDiscipline.getSort().longValue());
        partDisciplineService.insert(partDiscipline);
        return SUCCESS_TIP;
    }

    /**
     * 删除党风党纪管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long partDisciplineId) {
        partDisciplineService.deleteById(partDisciplineId);
        return SUCCESS_TIP;
    }

    /**
     * 修改党风党纪管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(PartDiscipline partDiscipline) {
    	if (ToolUtil.isOneEmpty(partDiscipline, partDiscipline.getId(), partDiscipline.getName(), partDiscipline.getContent())) {
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
        }
    	PartDiscipline old = this.partDisciplineService.selectById(partDiscipline.getId());
    	old.setCategory(partDiscipline.getCategory());
    	old.setContent(partDiscipline.getContent());
    	old.setName(partDiscipline.getName());
    	old.setSort(partDiscipline.getSort().longValue());
    	old.setCreatetime(new Date());
    	old.updateById();
        return SUCCESS_TIP;
    }

    /**
     * 党风党纪管理详情
     */
    @RequestMapping(value = "/detail/{partDisciplineId}")
    @ResponseBody
    public Object detail(@PathVariable("partDisciplineId") Long partDisciplineId) {
        return partDisciplineService.selectById(partDisciplineId);
    }
}
